#!/bin/env bash
BotName="$1"
Path=$2
AFHOME=$3
cd ${Path}
URL="https://ipinfo.io"
Address=$(curl -sL ${URL} | sed -n 's/.*"country": "\(.*\)",.*/\1/p')
if [ "${Address}" = "CN" ]
then
  GitMirror="gitee.com"
else
  GitMirror="github.com"
fi

install_Bot(){
  if [ "${BotName}" == "Miao-Yunzai" ]
  then
    Git=https://${GitMirror}/yoimiya-kokomi/Miao-Yunzai.git
  elif [ "${BotName}" == "TRSS-Yunzai" ]
  then
     Git=https://${GitMirror}/TimeRainStarSky/Yunzai.git
  fi
  echo -e ${cyan}正在克隆${BotName}${background}
  if ! git clone --depth=1 ${Git} $HOME/${BotName}
  then
    echo -e ${red}克隆失败${background}
    exit
  fi
}
install_Miao_Plugin(){
  echo -e ${cyan}正在克隆喵喵插件${background}
  Git=https://${GitMirror}/yoimiya-kokomi/miao-plugin.git
  if ! git clone --depth=1 ${Git} $HOME/${BotName}/plugins/miao-plugin;then
    echo -e ${red}克隆失败${background}
    exit
  fi
}
install_Genshin(){
  echo -e ${cyan}正在克隆原神组件${background}
  Git=https://${GitMirror}/TimeRainStarSky/Yunzai-genshin
  if ! git clone --depth=1 ${Git} $HOME/${BotName}/plugins/genshin;then
    echo -e ${red}克隆失败${background}
    exit
  fi
}
if [ "${BotName}" = "Miao-Yunzai" ];then
  if [ ! -e $HOME/${BotName}/package.json ];then
    install_Bot
  fi
  if [ ! -e $HOME/${BotName}/plugins/miao-plugin/index.js ];then
    install_Miao_Plugin
  fi
  if [ ! -d $HOME/${BotName}/node_modules ];then
    cd ${Path}/${BotName}
    bash $AFHOME/Install/Package
  fi
elif [ "${BotName}" = "TRSS-Yunzai" ];then
  if [ ! -e $HOME/${BotName}/package.json ];then
    install_Bot
  fi
  if [ ! -e $HOME/${BotName}/plugins/miao-plugin/index.js ];then
    install_Miao_Plugin
  fi
  if [ ! -e $HOME/${BotName}/plugins/genshin/index.js ];then
    install_Genshin
  fi
  if [ ! -d $HOME/${BotName}/node_modules ];then
    cd ${Path}/${BotName}
    bash $AFHOME/Install/Package
  fi
fi